'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
    // 用户列表
    async index() {
        let result = [];
        // 查询 多个
        let Op = this.app.Sequelize.Op;
        let page = this.ctx.query.page ? parseInt(this.ctx.query.page) : 1
        let limit = 5;
        let offset = (page - 1) * 5;
        result = await this.app.model.User.findAll();
        // 查询 多个并计数
        result = await this.app.model.User.findAndCountAll({
            where: {
                username: {
                    [Op.like]: "%e%"
                }
            },
            attributes: ['id', 'username'],
            order: [
                ['id', 'DESC']
            ],
            offset: offset,
            limit: limit,
        });
        this.ctx.body = {
            msg: "ok",
            data: result
        };
    }

    //   创建用户
    async create() {
        // 参数验证
        let params = this.ctx.request.body;
        this.ctx.validate({
            username : {
                type: 'string',
                required: true,
                desc: '姓名'
            },
            password : {
                type: 'string',
                required: true,
                desc: '密码'
            },
            class_id : {
                type: 'int',
                required: true,
                desc: '所在年级'
            },
            avatar : {
                type: 'string',
                required: false,
                desc: '头像'
            },
            coin : {
                type: 'number',
                required: false,
                desc: '金币'
            }
        })
        // 写入数据库
        let res = await this.app.model.User.create(params);
        // 成功
        this.ctx.body = res;
    }

    // 读取单个用户数据
    async read() {
        let id = parseInt(this.ctx.params.id);
        let detail = await this.app.model.User.findByPk(id);
        if (!detail) {
            return this.ctx.body = {
                msg: 'fail',
                data: '用户不存在'
            };
        }
        this.ctx.body = {
            msg: 'ok',
            data: detail
        };
    }

    // 更新用户
    async update() {
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        let data = await this.app.model.User.findByPk(id);
        if (!data) {
            return this.ctx.body = {
                msg: "fail",
                data: "该记录不存在"
            }
        }
        // data.username = "xiaozheng";
        // let res = await data.save();
        let params = this.ctx.request.body;
        let res = await data.update(params);
        this.ctx.body = {
            msg: "ok",
            data: res
        }


    }

    // 删除单个用户
    async destroy() {
        let id = this.ctx.params.id ? parseInt(this.ctx.params.id) : 0;
        let data = await this.app.model.User.findByPk(id);
        if (!data) {
            return this.ctx.body = {
                msg: "fail",
                data: "该记录不存在"
            }
        }
       let res = await data.destroy();
        this.ctx.body = {
            msg: "ok",
            data: res
        }
    }
}

module.exports = UserController;
